package com.kws.tool.controller;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.kws.common.core.constant.Constants;
import com.kws.common.core.domain.R;
import com.kws.common.core.utils.StringUtils;
import com.kws.common.core.utils.file.FileUtils;
import com.kws.tool.service.FileShiftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;

/**
 * @author :yxy
 * @date : 2024/11/21
 */
@Api(tags = "文件转换",value = "文件转换")
public class FileShiftController {

    @Autowired
    private FileShiftService fileShiftService;

    @PostMapping("/docxToPdf")
    @ApiOperation("doc、docx文件转pdf")
    public R docxToPdf(@RequestParam("file") MultipartFile file){
        fileShiftService.docxToPdf(file);
        return R.ok();
    }

   /* @CrossOrigin
    @GetMapping("/common/download/resourcePdf")
    public void resourceDownloadPdf(String resource, HttpServletRequest request, HttpServletResponse response) {
        try {
            resource = URLDecoder.decode(resource, "UTF-8");
            if (!FileUtils.checkAllowDownload(resource)) {
                throw new Exception(StringUtils.format("资源文件({})非法，不允许下载。 ", resource));
            }
            // 本地资源路径
            String localPath = RuoYiConfig.getProfile();
            // 数据库资源地址
            String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
            String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
            File inputWord = new File(downloadPath);
            File outputFile = new File(System.getProperty("user.dir") + "/uploadPath/pdf/" + downloadName.substring(0, downloadName.indexOf(".")) + ".pdf");
            // 下载名称
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            //返回转换之后的文件路径
            FileInputStream in = new FileInputStream(outputFile.getPath());
            response.setContentType(MediaType.APPLICATION_PDF_VALUE);
            OutputStream out = response.getOutputStream();
            byte[] b = new byte[512];
            while ((in.read(b)) != -1) {
                out.write(b);
            }
            out.flush();
            in.close();
            out.close();
        } catch (Exception e) {
            log.error("文件转换失败", e);
        }
    }*/

}
